package com.xujiang.myblog;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.xujiang.common.ui.CPBaseActivity;
import com.xujiang.myblog.adapter.MyPagerAdapter;
import com.xujiang.myblog.entity.TabEntity;
import com.xujiang.myblog.fragment.BlogFragment;
import com.xujiang.myblog.fragment.FriendFragment;
import com.xujiang.myblog.fragment.OtherFragment;
import com.xujiang.myblog.fragment.UserFragment;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends CPBaseActivity {

    @BindView(R.id.main_tab_view_pager)
    ViewPager mainTabViewPager;
    @BindView(R.id.main_tab_layout)
    CommonTabLayout mainTabLayout;
    @BindView(R.id.activity_main)
    FrameLayout activityMain;
    private String[] mTitles = {"首页", "消息", "联系人", "更多"};

    private int[] mIconUnselectIds = {
            R.mipmap.tab_home_unselect, R.mipmap.tab_speech_unselect,
            R.mipmap.tab_contact_unselect, R.mipmap.tab_more_unselect};
    private int[] mIconSelectIds = {
            R.mipmap.tab_home_select, R.mipmap.tab_speech_select,
            R.mipmap.tab_contact_select, R.mipmap.tab_more_select};

    private Fragment[] mFragments = {
            new BlogFragment(), new FriendFragment(),
            new OtherFragment(), new UserFragment()};

    private View mDecorView;

    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();


    public static void logInOut(Context context) {
        Intent intent = new Intent(context, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        context.startActivity(intent);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]));
        }

        mainTabViewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager(), mFragments));
        //通过decorView获取到程序显示的区域，包括标题栏，但不包括状态栏。间接可以计算状态栏高度。
        //decorView是window中的最顶层view
        //当需要在其他类中对view和id进行绑定时需要，这里不适用
        mDecorView = getWindow().getDecorView();

        mainTabLayout.setTabData(mTabEntities);

        mainTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                mainTabViewPager.setCurrentItem(position);
                Log.i("test",position+"");
            }

            @Override
            public void onTabReselect(int position) {
               //选中时再点击调用
            }
        });
        /**
         * viewpager每次切换的时候， 会重新创建当前界面及左右界面三个界面， 每次切换都要重新oncreate, 所以只要设置viewPager setOffscreenPageLimit即可避免这个问题。
         viewPager.setOffscreenPageLimit(3);表示三个界面之间来回切换都不会重新加载
         * */
        /**
         * 通过setOffscreenPageLimit(0)来设置不提前预加载，但这样的设置没有效果。
          通过研究ViewPager的 源码可以设置该类默认的DEFAULT_OFFSCREEN_PAGES = 0，来预防预加载
         * */
        mainTabViewPager.setOffscreenPageLimit(1);
        mainTabViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                mainTabLayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }
        });

    }

    protected int dp2px(float dp){
        final float scale = this.getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }
}
